package search

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"wechat/global"
	"wechat/handle/model"
	"wechat/handle/request"
)

// 朋友圈发布
func AddMom(c *gin.Context) {
	var form request.AddMom
	if err := c.ShouldBind(&form); err != nil {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}

	var moment model.Moment
	moment = model.Moment{
		UserId:  form.UserId,
		Content: form.Content,
		Count:   form.Count,
	}

	if err := moment.AddMoment(global.DB); err != nil {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "朋友圈发布失败",
		})
		return
	}

	c.JSONP(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "朋友圈发布成功",
	})

}

//朋友圈列表

func ListMom(c *gin.Context) {
	var form request.ListMom
	if err := c.ShouldBind(&form); err != nil {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		return
	}

	var mom model.Moment

	listMessage, err := mom.ListMoment(global.DB, form.UserId)
	if err != nil {
		c.JSONP(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "朋友圈列表获取失败",
		})
		return
	}

	c.JSONP(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "朋友圈列表获取成功",
		"Data": map[string]interface{}{
			"list": listMessage,
		},
	})

}
